<?php
/**
 * Created by PhpStorm.
 * User: jsq
 * Date: 2017/12/2 0002
 * Time: 16:36
 */

namespace app\admin\controller;

use app\admin\logic\Kindergarten as Kindergarten_logic;
use think\Cache;
use think\Db;

class Kindergarten extends Admin
{
    /**
     * 幼儿园列表
     * @return mixed
     */
    public function index(){
        $kindergarten_logic = new Kindergarten_logic();
        $kindergartena = $kindergarten_logic->getAllKindergartens();
        $this->assign('kindergartena',$kindergartena);
        return $this->fetch();
    }

    /**
     * 新增幼儿园
     * @return mixed
     */
    public function add(){
        if($this->request->isPost()){
            $data=input('post.');
            $kindergarten_logic = new Kindergarten_logic();
            $result = $kindergarten_logic->addKindergarten($data);
            if($result['result'] == 1){
                $this->success($result['msg']);
            }else{
                $this->error($result['msg']);
            }
        }else{
            $area_logic = new \app\admin\logic\Area();
            $areas = $area_logic->getEnabledAreas();
            $this->assign('areas',$areas);
            return $this->fetch();
        }
    }

    /**
     * 编辑幼儿园
     * @return mixed
     */
    public function edit(){
        if($this->request->isPost()){
            $data=input('post.');
            $kindergarten_logic = new Kindergarten_logic();
            $result = $kindergarten_logic->editKindergarten($data);
            if($result['result'] == 1){
                $this->success($result['msg']);
            }else{
                $this->error($result['msg']);
            }
        }else{
            $id=input('id');
            $kindergarten = Db::name('kindergarten')->where('id',$id)->find();
            $area_logic = new \app\admin\logic\Area();
            $areas = $area_logic->getEnabledAreas();
            $this->assign('areas',$areas);
            $this->assign('kindergarten',$kindergarten);
            return $this->fetch();
        }
    }

    /**
     * 删除幼儿园
     */
    public function del()
    {
        $id = $this->request->param('id', 0);
        $kindergarten_logic = new Kindergarten_logic();
        $result = $kindergarten_logic->delKindergarten($id);
        if ($result['result'] === true) {
            $this->success($result['msg']);
        } else {
            $this->error($result['msg']);
        }
    }


    /**
     * 设置幼儿园启用和禁用
     * @return \think\response\Json
     */
    public function setStatus(){
        $id = input('id');
        if(empty($id)){
            $this->error('ID丢失');
        }
        $info = Db::name('kindergarten')->where('id',$id)->field('id,status')->find();
        $status = [1,0];
        $data['id'] = $id;
        $data['status'] = $status[$info['status']];
        $res = Db::name('kindergarten')->update($data);
        if($res){
            Cache::rm('allKindergartens');
            $this->success('设置成功');
        }else{
            $this->error('设置失败');
        }
    }



}